package com.mall.server.service.sms;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.autoconfig.exception.BusinessException;
import com.mall.dubbo.api.SmsHomeBrandApi;
import com.mall.model.domain.SmsHomeBrand;
import com.mall.model.vo.CommonPage;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Service
public class SmsHomeBrandService {

    @DubboReference
    private SmsHomeBrandApi smsHomeBrandApi;

    //分页查询推荐品牌
    public CommonPage list(Integer pageNum, Integer pageSize, SmsHomeBrand homeBrand) {

        String brandName = homeBrand.getBrandName();
        Integer recommendStatus = homeBrand.getRecommendStatus();

        Page<SmsHomeBrand> page = smsHomeBrandApi.listPage(pageNum, pageSize, brandName, recommendStatus);

        return new CommonPage(page);
    }

    //添加首页推荐品牌
    public void create(List<SmsHomeBrand> homeBrandList) {

        for (SmsHomeBrand smsHomeBrand : homeBrandList) {
            Random r = new Random();
            int sort = r.nextInt(100) + 50;
            smsHomeBrand.setSort(sort);
            smsHomeBrand.setRecommendStatus(1);
        }

        smsHomeBrandApi.saveBatch(homeBrandList);
    }

    public void delete(List<Long> ids) {

        smsHomeBrandApi.removeByIds(ids);
    }

    //批量修改推荐品牌状态
    public void update(List<Long> ids, SmsHomeBrand smsHomeBrand) {

        List<SmsHomeBrand> list = smsHomeBrandApi.findByIds(ids, smsHomeBrand);

        if (!CollUtil.isEmpty(list)) {
            throw new BusinessException("这是错误的操作");
        }
        smsHomeBrandApi.updateBYIds(ids, smsHomeBrand);
    }

    //修改推荐品牌排序
    public void updateSort(Integer id, SmsHomeBrand homeBrand) {

        smsHomeBrandApi.updateSort(id, homeBrand);
    }

}
